/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.ui.composites;

import java.util.Iterator;

import org.eclipse.gef.EditPart;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipse.uml2.uml.Action;
import org.eclipse.uml2.uml.ActionExecutionSpecification;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagram.sequence.ui.dialogs.ExecutionSpecificationActionGenericActionTreeSelectionDialog;
import com.cea.papyrus.tabbedproperties.composites.DecoratedLabelComposite;


public class ExecutionSpecificationActionComposite extends DecoratedLabelComposite {

	/**
	 * debug channel.
	 */
	protected static int channel = 501;

	/**
	 * behavior selected
	 */
	protected Action action = null;
	
	/**
	 * Default Constructor.
	 */
	public ExecutionSpecificationActionComposite(){
		super("Action");
	}

	/**
	 * Constructor with given name for label.
	 */
	public ExecutionSpecificationActionComposite(String name){
		super(name);
	}

	/**
	 * Property page
	 */
	private TabbedPropertySheetPage tabbedPropertySheetPage = null;
	
	/**
	 * Loads a new property page on the effect
	 */
	@Override
	public void editText(){
		//TODO: find a solution to load a new property page
		if(action != null){
			EditPart selection = null;
			
			Iterator<EditPart> i = LookForElement.getAllOutlineEditParts().iterator();
			while((selection == null) && i.hasNext()){
				EditPart current = i.next() ;
				if((current.getModel() != null) && current.getModel().equals(action))
					selection = current ;
			}
			if(selection != null){
				tabbedPropertySheetPage.selectionChanged(
						getActiveEditor(), 
						new StructuredSelection(selection));
			}
		}
	}

	/**
	 * Stores the tabbed property page before handling the call
	 * to createContent
	 */
	public Composite createContent(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage){
		this.tabbedPropertySheetPage = tabbedPropertySheetPage;
		return createContent(parent, tabbedPropertySheetPage.getWidgetFactory());
	}

	/**
	 * Creates contents.
	 * 
	 * @param parent Composite.
	 * @param factory TabbedPropertySheetWidgetFactory.
	 * 
	 * @return specific Composite.
	 */
	@Override
	public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) {
		Composite composite = super.createContent(parent, factory);

		label.setText("Action:");
		label.setToolTipText("Action");
		
		return composite;
	}

	/**
	 * Refreshes the composite.
	 */
	@Override
	public void refresh() {
		
		Element elt = element.getUml2Element();
		
		assert elt != null : 
			"Passed argument is null";
		assert elt instanceof ActionExecutionSpecification : 
			"Passed argument is not a ActionExecutionSpecification";

		action = ((ActionExecutionSpecification) element.getUml2Element()).getAction();
		if(action != null){
			if(!text.isDisposed()){
				text.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(action));
				text.setText(action.getLabel());
			}
			if(!addButton.isDisposed())
				addButton.setEnabled(false);
			if(!removeButton.isDisposed())
				removeButton.setEnabled(true);
		}
		else{
			if(!text.isDisposed()){
				text.setText("Action Unset");
				text.setImage(null);
			}
			if(!addButton.isDisposed())
				addButton.setEnabled(true);
			if(!removeButton.isDisposed())
				removeButton.setEnabled(false);
		}
	}

	/**
	 * Executed behavior when addButton pressed.
	 */
	public void addButtonPressed() {
		Element elt = element.getUml2Element();
		
		assert elt != null : 
			"Passed argument is null";
		assert elt instanceof ActionExecutionSpecification : 
			"Passed argument is not a ActionExecutionSpecification";

		int result;			
		ExecutionSpecificationActionGenericActionTreeSelectionDialog dialog = 
			new ExecutionSpecificationActionGenericActionTreeSelectionDialog(
					composite.getShell(), 
					(ActionExecutionSpecification) element.getUml2Element());
		result = dialog.open();
		
		// result treatment
		if (result == IDialogConstants.OK_ID) {
			org.eclipse.uml2.uml.Action action = dialog.getResult();
							
			if(action != null) {
				((ActionExecutionSpecification) element.getUml2Element()).setAction(action);
			}
			refresh();
		}
		
		dialog.removeListeners();
	}
	
	/**
	 * Executed behavior when removeButton pressed.
	 */
	public void removeButtonPressed(){
		Element elt = element.getUml2Element();
		
		assert elt != null : 
			"Passed argument is null";
		assert elt instanceof ActionExecutionSpecification : 
			"Passed argument is not a ActionExecutionSpecification";

		if(!text.isDisposed())
			text.setText("Action Unset");

		action = ((ActionExecutionSpecification) element.getUml2Element()).getAction();
		if(action != null){
			((ActionExecutionSpecification) element.getUml2Element()).setAction(null);
		}

		//refresh contents
		refresh();
	}

	/**
	 * Disposes of the composite
	 */
	public void dispose(){
		super.dispose();
	}
}
